﻿using System;
using Ooaavee.Support;

namespace DevelopersTestPad.Remoting
{
    /// <summary>
    /// Extension methods.
    /// </summary>
    public static class ExtensionEngine
    {
        private static DevelopersTestPadRemoteScript _instance;
        private static DevelopersTestPadParameters _parameters;

        /// <summary>
        /// Initializes the extension engine. This method is for the internal use only!.
        /// </summary>
        /// <param name="instance">The remote script instance.</param>
        /// <param name="parameters">Parameters.</param>
        internal static void Initialize(DevelopersTestPadRemoteScript instance, DevelopersTestPadParameters parameters)
        {
            _instance = instance;
            _parameters = parameters;
        }

        public static TTargetType Out<TTargetType>(this TTargetType target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            Send(InteractionMessageEventArgs.InteractionMessageType.Text, target.ToString());

            return target;
        }

        public static TTargetType Push<TTargetType>(this TTargetType target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            Reflector.ReflectedObject obj = Reflector.GetReflectedObject(target);

            Send(InteractionMessageEventArgs.InteractionMessageType.Push, obj);

            return target;
        }

        private static void Send(InteractionMessageEventArgs.InteractionMessageType type, object data)
        {
            _parameters.OnReceiveInteractionMessage(new InteractionMessageEventArgs { Type = type, Data = data });            
        }

    }
}
